﻿/*
Copyright (c) 2011 <a href="http://www.gutgames.com">James Craig</a>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

#region Usings
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using ObjectCartographer.Interfaces;
using ObjectCartographer.Mappings.DataClasses;
using Utilities.DataTypes.Patterns;
#endregion

namespace ObjectCartographer.Mappings
{
    /// <summary>
    /// Maps a type to another type
    /// </summary>
    public class TypeMapping : Factory<Type, object>, ITypeMapping
    {
        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public TypeMapping()
        {
            Setup();
        }

        #endregion

        #region Private Functions

        /// <summary>
        /// Sets up the mappings
        /// </summary>
        private void Setup()
        {
            if (PropertyMappings != null)
                return;
            PropertyMappings = new Dictionary<Type, MappingInfo>();
        }

        #endregion

        #region Protected Functions

        /// <summary>
        /// Maps a type to another type
        /// </summary>
        /// <typeparam name="Source">Source type</typeparam>
        /// <typeparam name="Destination">Destination type</typeparam>
        /// <param name="DestinationType">Function for creating the destination type</param>
        /// <param name="DestinationProperty">Property to map to</param>
        protected void Map<Source, Destination>(Func<object> DestinationType,
            Expression<Func<Destination, object>> DestinationProperty)
        {
            Setup();
            Register(typeof(Source), DestinationType);
            PropertyMappings.Add(typeof(Source), new MappingInfo("", Utilities.Reflection.Reflection.GetPropertyName<Destination>(DestinationProperty), "",""));
        }

        /// <summary>
        /// Maps a type to another type
        /// </summary>
        /// <typeparam name="Source">Source type</typeparam>
        /// <typeparam name="Destination">Destination type</typeparam>
        /// <param name="DestinationType">Function for creating the destination type</param>
        /// <param name="DestinationProperty">Property to map to</param>
        /// <param name="Format">Format string</param>
        protected void Map<Source, Destination>(Func<object> DestinationType,
            Expression<Func<Destination, object>> DestinationProperty,
            string Format)
        {
            Setup();
            Register(typeof(Source), DestinationType);
            PropertyMappings.Add(typeof(Source), new MappingInfo("", Utilities.Reflection.Reflection.GetPropertyName<Destination>(DestinationProperty), Format, ""));
        }

        /// <summary>
        /// Maps a description to another type
        /// </summary>
        /// <typeparam name="Destination">Destination type</typeparam>
        /// <param name="DestinationType">Function for creating the destination type</param>
        /// <param name="DestinationProperty">Property to map to</param>
        protected void MapDescription<Destination>(Func<object> DestinationType,
            Expression<Func<Destination, object>> DestinationProperty)
        {
            MapDescription<Destination>(DestinationType, DestinationProperty, "");
        }

        /// <summary>
        /// Maps a description to another type
        /// </summary>
        /// <typeparam name="Destination">Destination type</typeparam>
        /// <param name="DestinationType">Function for creating the destination type</param>
        /// <param name="DestinationProperty">Property to map to</param>
        /// <param name="Format">Formatting string</param>
        protected void MapDescription<Destination>(Func<object> DestinationType,
            Expression<Func<Destination, object>> DestinationProperty,
            string Format)
        {
            Register(typeof(MappingInfo), DestinationType);
            DescriptionMapping = new MappingInfo("", Utilities.Reflection.Reflection.GetPropertyName<Destination>(DestinationProperty), Format, "");
        }

        #endregion

        #region Public Functions

        /// <summary>
        /// Creates the destination type
        /// </summary>
        /// <param name="SourceValue">Source value</param>
        /// <param name="SourceType">Source type</param>
        /// <returns>The destination type</returns>
        public object CreateDestination(object SourceValue, Type SourceType)
        {
            object DestinationObject = Create(SourceType);
            Utilities.Reflection.Reflection.SetValue(SourceValue, DestinationObject, PropertyMappings[SourceType].Destination, PropertyMappings[SourceType].Format);
            return DestinationObject;
        }

        /// <summary>
        /// Creates a description object based off of the description input
        /// </summary>
        /// <param name="DescriptionValue">The description value</param>
        /// <returns>The description object</returns>
        public object CreateDescription(object DescriptionValue)
        {
            object DestinationObject = Create(typeof(MappingInfo));
            Utilities.Reflection.Reflection.SetValue(DescriptionValue, DestinationObject, DescriptionMapping.Destination, DescriptionMapping.Format);
            return DestinationObject;
        }

        /// <summary>
        /// Syncs the source to the destination
        /// </summary>
        /// <param name="SourceObject">Source object</param>
        /// <param name="SourcePropertyInfo">Property to copy</param>
        /// <param name="DestinationObject">Destination object</param>
        public void Sync(object SourceObject, PropertyInfo SourcePropertyInfo, object DestinationObject)
        {
            object Value = Utilities.Reflection.Reflection.GetPropertyValue(DestinationObject, PropertyMappings[SourcePropertyInfo.PropertyType].Destination);
            Utilities.Reflection.Reflection.SetValue(Value, SourceObject, SourcePropertyInfo, PropertyMappings[SourcePropertyInfo.PropertyType].Format);
        }

        #endregion

        #region Internal Properties

        internal Dictionary<Type, MappingInfo> PropertyMappings { get; set; }
        internal MappingInfo DescriptionMapping { get; set; }

        #endregion

        #region Internal Functions

        /// <summary>
        /// Gets a list of items that are not of the description object's type
        /// </summary>
        /// <param name="TempDestinationList">List to sort through</param>
        /// <returns>A list of items that are not descriptions</returns>
        internal List<object> GetNonDescriptions(List<object> TempDestinationList)
        {
            List<object> TempList = new List<object>();
            Type Temp=Create(typeof(MappingInfo)).GetType();
            foreach (object TempDestination in TempDestinationList)
            {
                if (TempDestination.GetType() != Temp)
                {
                    TempList.Add(TempDestination);
                }
            }
            return TempList;
        }

        #endregion
    }
}
